<template>
  <div class="login-container">
    <div class="background-overlay"></div>
    <div class="login-box">
      <div class="login-header">
        <h1>{{ isRegister ? '注册' : '登录' }}</h1>
        <p>{{ isRegister ? '创建新账户' : '欢迎回来，请登录您的账户' }}</p>
      </div>

      <form @submit.prevent="handleSubmit" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <div class="input-wrapper">
            <i class="fas fa-user input-icon"></i>
            <input
              id="username"
              v-model="username"
              type="text"
              placeholder="请输入用户名"
              required
              autocomplete="username"
              pattern="^[a-zA-Z0-9_]+$"
            />
          </div>
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <div class="input-wrapper">
            <i class="fas fa-lock input-icon"></i>
            <input
              id="password"
              v-model="password"
              type="password"
              placeholder="请输入密码"
              required
              :autocomplete="isRegister ? 'new-password' : 'current-password'"
              minlength="6"
            />
          </div>
        </div>

        <div v-if="isRegister" class="form-group">
          <label for="email">邮箱（可选）</label>
          <div class="input-wrapper">
            <i class="fas fa-envelope input-icon"></i>
            <input
              id="email"
              v-model="email"
              type="email"
              placeholder="请输入邮箱"
              autocomplete="email"
            />
          </div>
        </div>

        <div v-if="isRegister" class="form-group">
          <label for="phone">手机号（可选）</label>
          <div class="input-wrapper">
            <i class="fas fa-phone input-icon"></i>
            <input
              id="phone"
              v-model="phone"
              type="tel"
              placeholder="请输入手机号"
              autocomplete="tel"
              pattern="^1[3-9]\d{9}$"
            />
          </div>
        </div>

        <div v-if="successMessage" class="success-message">
          <i class="fas fa-check-circle"></i>
          {{ successMessage }}
        </div>
        <div v-if="errorMessage" class="error-message">
          <i class="fas fa-exclamation-circle"></i>
          {{ errorMessage }}
        </div>

        <button type="submit" :disabled="loading" class="login-button">
          <span v-if="loading">{{ isRegister ? '注册中...' : '登录中...' }}</span>
          <span v-else>{{ isRegister ? '注册' : '登录' }}</span>
        </button>

        <div class="switch-mode">
          <span>{{ isRegister ? '已有账户？' : '还没有账户？' }}</span>
          <a href="#" @click.prevent="toggleMode" class="switch-link">
            {{ isRegister ? '立即登录' : '立即注册' }}
          </a>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import api from '@/utils/api'

const router = useRouter()
const authStore = useAuthStore()

const isRegister = ref(false)
const username = ref('')
const password = ref('')
const email = ref('')
const phone = ref('')
const loading = ref(false)
const errorMessage = ref('')
const successMessage = ref('')

const toggleMode = () => {
  isRegister.value = !isRegister.value
  errorMessage.value = ''
  // 清空表单（保留用户名）
  password.value = ''
  email.value = ''
  phone.value = ''
}

const handleSubmit = async () => {
  errorMessage.value = ''
  loading.value = true

  try {
    if (isRegister.value) {
      // 注册 - 直接调用API，不通过store（避免自动设置token）
      const response = await api.post('/auth/register', {
        username: username.value,
        password: password.value,
        email: email.value || undefined,
        phone: phone.value || undefined
      })
      
      if (response.data.code === 200) {
        // 注册成功，切换到登录模式并清空密码字段，保留用户名
        isRegister.value = false
        password.value = ''
        email.value = ''
        phone.value = ''
        errorMessage.value = ''
        // 显示成功提示
        successMessage.value = '注册成功！请登录'
        setTimeout(() => {
          successMessage.value = ''
        }, 3000)
      } else {
        throw new Error(response.data.message || '注册失败')
      }
    } else {
      // 登录
      await authStore.login(username.value, password.value)
      // 登录成功，跳转到首页
      router.push('/')
    }
  } catch (error: any) {
    errorMessage.value = error.response?.data?.message || (isRegister.value ? '注册失败，请检查输入信息' : '登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  position: relative;
  background: #0a1929;
  overflow: hidden;
}

/* 背景图片样式 */
.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('/login-bg.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  opacity: 0.7;
  z-index: 0;
  transform: scale(1);
  transition: transform 0.3s ease;
}

.login-container:hover::before {
  transform: scale(1.05);
}

.background-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(10, 25, 41, 0.25);
  z-index: 1;
}

.login-box {
  background: rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(15px) saturate(180%);
  border-radius: 20px;
  box-shadow: 0 25px 80px rgba(0, 0, 0, 0.3);
  padding: 3rem;
  width: 100%;
  max-width: 420px;
  position: relative;
  z-index: 2;
  color: white;
}

.login-header {
  text-align: center;
  margin-bottom: 2.5rem;
}

.login-header h1 {
  font-size: 2.2rem;
  font-weight: 700;
  color: white;
  margin-bottom: 0.5rem;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.login-header p {
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.95rem;
  text-shadow: 0 1px 5px rgba(0, 0, 0, 0.2);
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.form-group label {
  font-weight: 600;
  color: white;
  font-size: 0.9rem;
  margin-left: 0.25rem;
  text-shadow: 0 1px 5px rgba(0, 0, 0, 0.2);
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 1rem;
  color: #87CEEB;
  font-size: 1rem;
  z-index: 1;
  transition: color 0.3s ease;
}

.form-group input {
  width: 100%;
  padding: 0.875rem 1rem 0.875rem 2.75rem;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(5px);
}

.form-group input:focus {
  outline: none;
  border: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  transform: translateY(-1px);
  background: rgba(255, 255, 255, 0.25);
}

.form-group input:focus + .input-icon,
.form-group:focus-within .input-icon {
  color: #B0E0E6;
}

.form-group input::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.success-message {
  padding: 0.875rem 1rem;
  background: linear-gradient(135deg, #c6f6d5 0%, #9ae6b4 100%);
  color: #22543d;
  border-radius: 10px;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  border: 1px solid rgba(34, 84, 61, 0.2);
}

.success-message i {
  font-size: 1rem;
}

.error-message {
  padding: 0.875rem 1rem;
  background: linear-gradient(135deg, #fed7d7 0%, #fc8181 100%);
  color: #c53030;
  border-radius: 10px;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  border: 1px solid rgba(197, 48, 48, 0.2);
}

.error-message i {
  font-size: 1rem;
}

.login-button {
  padding: 0.875rem;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
  position: relative;
  overflow: hidden;
}

.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s ease;
}

.login-button:hover:not(:disabled)::before {
  left: 100%;
}

.login-button:hover:not(:disabled) {
  background: #5568d3;
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.login-button:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.login-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.switch-mode {
  text-align: center;
  margin-top: 1rem;
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.9rem;
  text-shadow: 0 1px 5px rgba(0, 0, 0, 0.2);
}

.switch-link {
  color: white;
  text-decoration: none;
  margin-left: 0.5rem;
  font-weight: 600;
  transition: all 0.3s ease;
  text-shadow: 0 1px 5px rgba(0, 0, 0, 0.2);
}

.switch-link:hover {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: underline;
}

@media (max-width: 768px) {
  .login-container {
    justify-content: center;
    padding: 1rem;
  }
  
  .login-box {
    padding: 2rem 1.5rem;
    max-width: 100%;
  }
  
  .login-header h1 {
    font-size: 1.8rem;
  }
  
  .login-container::before {
    background-size: cover;
    background-position: center;
  }
}
</style>

